package com.xiaolei.baselib.util;

import java.lang.reflect.Field;

import android.app.Activity;
import android.content.SharedPreferences;

import com.xiaolei.baselib.annotation.AutoWrite;
import com.xiaolei.baselib.annotation.Editor;
import com.xiaolei.baselib.annotation.SharedPreference;
import com.xiaolei.baselib.annotation.findViewById;
import com.xiaolei.baselib.annotation.getIntent;
import com.xiaolei.baselib.base.BaseActivity;
import com.xiaolei.baselib.base.BaseFragment;
import com.xiaolei.baselib.base.BaseV4Fragment;

/**
 * 自定义注解的核心转换器
 */
public class AnnotationUtil
{
	/**
	 * BaseActivity中调用此方法解析注解
	 * 
	 * @param activity
	 * @param field
	 * @return
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 */
	public static Object getAnnotationObject(Activity activity, Field field) throws IllegalAccessException,
			InstantiationException
	{
		Object object = null;
		// java.lang.IllegalAccessException: access to field not allowed
		if (field.isAnnotationPresent(findViewById.class))// 对View进行注入
		{
			findViewById findid = field.getAnnotation(findViewById.class);
			int id = findid.value();
			object = activity.findViewById(id);
		} else if (field.isAnnotationPresent(SharedPreference.class))// 对SharedPreference进行注入
		{
			SharedPreference findid = field.getAnnotation(SharedPreference.class);
			String name = findid.value();
			SharedPreferences sharedPreferences = activity.getSharedPreferences(name, Activity.MODE_PRIVATE);
			object = sharedPreferences;
		} else if (field.isAnnotationPresent(SharedPreference.getString.class)) // 在SharedPreference中取出字符串
		{
			SharedPreference.getString findid = field.getAnnotation(SharedPreference.getString.class);
			String config = findid.config();
			String name = findid.name();
			SharedPreferences sharedPreferences = activity.getSharedPreferences(config, Activity.MODE_PRIVATE);
			object = sharedPreferences.getString(name, "");
		} else if (field.isAnnotationPresent(SharedPreference.getBoolean.class)) // 在SharedPreference中取出bool
		{
			SharedPreference.getBoolean findid = field.getAnnotation(SharedPreference.getBoolean.class);
			String config = findid.config();
			String name = findid.name();
			SharedPreferences sharedPreferences = activity.getSharedPreferences(config, Activity.MODE_PRIVATE);
			object = sharedPreferences.getBoolean(name, false);
		} else if (field.isAnnotationPresent(SharedPreference.getFloat.class)) // 在SharedPreference中取出小数
		{
			SharedPreference.getFloat findid = field.getAnnotation(SharedPreference.getFloat.class);
			String config = findid.config();
			String name = findid.name();
			SharedPreferences sharedPreferences = activity.getSharedPreferences(config, Activity.MODE_PRIVATE);
			object = sharedPreferences.getFloat(name, 0.0f);
		} else if (field.isAnnotationPresent(SharedPreference.getInt.class)) // 在SharedPreference中取出整数
		{
			SharedPreference.getInt findid = field.getAnnotation(SharedPreference.getInt.class);
			String config = findid.config();
			String name = findid.name();
			SharedPreferences sharedPreferences = activity.getSharedPreferences(config, Activity.MODE_PRIVATE);
			object = sharedPreferences.getInt(name, 0);
		} else if (field.isAnnotationPresent(Editor.class))// 对SharedPreference.Editor进行注入
		{

			Editor editor = field.getAnnotation(Editor.class);
			String name = editor.value();
			SharedPreferences sharedPreferences = activity.getSharedPreferences(name, Activity.MODE_PRIVATE);
			SharedPreferences.Editor d = sharedPreferences.edit();
			object = d;
		} else if (field.isAnnotationPresent(AutoWrite.class))// 是普通的注解
		{
			AutoWrite autowrite = field.getAnnotation(AutoWrite.class);
			Class clazz = autowrite.value();
			if (clazz == Void.class)
			{
				clazz = field.getType();
			}
			object = clazz.newInstance();
		} else if (field.isAnnotationPresent(getIntent.class)) // 获取传过来的intent
		{
			object = activity.getIntent();
		} else if (field.isAnnotationPresent(getIntent.getString.class))// 从intent中直接获取值注入
		{
			getIntent.getString getString = field.getAnnotation(getIntent.getString.class);
			object = activity.getIntent().getStringExtra(getString.value());
		} else if (field.isAnnotationPresent(getIntent.getInt.class))// 从intent中直接获取值注入
		{
			getIntent.getInt getint = field.getAnnotation(getIntent.getInt.class);
			object = activity.getIntent().getIntExtra(getint.name(), getint.defaultValue());
		} else if (field.isAnnotationPresent(getIntent.getSerializableExtra.class))// 从intent中直接获取值注入
		{
			getIntent.getSerializableExtra item = field.getAnnotation(getIntent.getSerializableExtra.class);
			object = activity.getIntent().getSerializableExtra(item.value());
		} else if (field.isAnnotationPresent(getIntent.getBoolean.class))// 从intent中直接获取值注入
		{
			getIntent.getBoolean item = field.getAnnotation(getIntent.getBoolean.class);
			object = activity.getIntent().getBooleanExtra(item.name(), item.defaultValue());
		}
		return object;
	}

	/**
	 * BaseFragment中调用此方法解析注解
	 * 
	 * @param fragment
	 * @param field
	 * @return
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 */
	public static Object getFragmentAnnotationObject(BaseFragment fragment, Field field) throws IllegalAccessException,
			InstantiationException
	{
		Object object = null;
		// java.lang.IllegalAccessException: access to field not allowed
		if (field.isAnnotationPresent(findViewById.class))// 对View进行注入
		{
			findViewById findid = field.getAnnotation(findViewById.class);
			int id = findid.value();
			object = fragment.getView().findViewById(id);
		} else
		{
			object = getAnnotationObject(fragment.getBaseActivity(), field);
		}
		return object;
	}
	
	/**
	 * BaseV4Fragment中调用此方法解析注解
	 * @param fragment
	 * @param field
	 * @return
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 */
	public static Object getFragmentAnnotationObject(BaseV4Fragment fragment, Field field) throws IllegalAccessException,
	InstantiationException
	{
		Object object = null;
		// java.lang.IllegalAccessException: access to field not allowed
		if (field.isAnnotationPresent(findViewById.class))// 对View进行注入
		{
			findViewById findid = field.getAnnotation(findViewById.class);
			int id = findid.value();
			object = fragment.getView().findViewById(id);
		} else
		{
			object = getAnnotationObject(fragment.getBaseActivity(), field);
		}
		return object;
	}

}
